package hdfs.api;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsDemo {
    public static void mkdirs() {
        // 1 获取文件系统
        Configuration conf = new Configuration();

        try (FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.39.151:9000"), conf, "root");) {
            // 2 创建目录
            fs.mkdirs(new Path("/hadoop/hdfs"));
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void copyFromLocalFile() {
        // 1 获取文件系统
        Configuration conf = new Configuration();

        try (FileSystem fs = FileSystem.get(new URI("hdfs://192.168.39.151:9000"), conf, "root");){
            // 2 上传文件
            // 从本地将文件拷贝到HDFS中，如果目标文件已存在则进行覆盖
            fs.copyFromLocalFile(new Path("d:/hdfs_data.txt"),
                    new Path("/hadoop/hdfs/hdfs_data.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public static void copyToLocalFile() {
        // 1 获取文件系统
        Configuration conf = new Configuration();

        try (FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.39.151:9000"), conf, "root");){
            // 2 执行下载操作
            fs.copyToLocalFile(new Path("/hadoop/hdfs/hdfs_data.txt"),
                    new Path("d:/test_data.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void delete() {
        // 1 获取文件系统
        Configuration conf = new Configuration();

        try (FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.39.151:9000"), conf, "root");){
            // 2 执行删除
            fs.delete(new Path("/hadoop"), true);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void rename() {
        // 1 获取文件系统
        Configuration conf = new Configuration();

        try (FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.39.151:9000"), conf, "root");){
            // 2 修改文件名称
            fs.rename(new Path("/hadoop/hdfs/hdfs_data.txt"),
                    new Path("/hadoop/hdfs/test_data.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void listFiles() {
        // 1 获取文件系统
        Configuration conf = new Configuration();

        try (FileSystem fs = FileSystem.get(URI.create("hdfs://172.16.224.222:9000"), conf, "root");){
            // 2 获取文件详情
            RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
            while (listFiles.hasNext()) {
                LocatedFileStatus status = listFiles.next();

                // 输出详情
                // 文件名称
                System.out.println(status.getPath().getName());

                // 长度
                System.out.println(status.getLen());

                // 权限
                System.out.println(status.getPermission());

                // 分组
                System.out.println(status.getGroup());

                // 获取存储的块信息
                BlockLocation[] blockLocations = status.getBlockLocations();

                for (BlockLocation blockLocation : blockLocations) {
                    // 获取块存储的主机节点
                    String[] hosts = blockLocation.getHosts();
                    for (String host : hosts) {
                        System.out.println(host);
                    }
                }
                System.out.println("-----------分割线----------");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        mkdirs();
//        copyFromLocalFile();
//        copyToLocalFile();
//        delete();
//        rename();
//        listFiles();
    }
}
